<template>
  <div>
    <div class="search_form">
			<div>
        <el-select
					size="mini"
					clearable
					placeholder="请选择船名航次"
					v-model="searchForm.shipvoyageId"
					class="others"
					popper-class="selectClass"
				>
					<el-option v-for="(item, index) in shipList" :key="index" :label="item.shipvoyageName" :value="item.shipvoyageId"></el-option>
				</el-select>
				<el-button @click="searchData" class="search_btn" type="primary" icon="el-icon-search" size="mini">
					查询
				</el-button>
			</div>
		</div>
    <div class="search_form">
      <div style="padding-left: 10px">
        调整前：<el-input size="mini" v-model="beforeAdject" disabled class="others"></el-input>
        调整后：<el-input size="mini" v-model="afterAdject" class="others" clearable></el-input>
        <el-button size="mini" class="search_btn" v-hasPermi="['yardManagement:TicketAdjustment:update']" type="primary" icon="el-icon-set-up" @click="autoAdject">自动调整</el-button>
        <el-checkbox style="padding-left: 10px" v-model="ischecked">只调整港存 不调整作业票</el-checkbox>
      </div>
    </div>
    <baseTablecheck
      ref="tableData"
      :loading="loading"
      :data="tableData"
      :columns="columns"
      :tableHT="tableHeight"
      :pagination="false"
      @checkId="selectChange"
    ></baseTablecheck>
  </div>
</template>

<script>
import api from "@/api/yardTally/ticketAdjustment/index"
import publicapi from "@/api/public/index"
import baseTablecheck from "@/components/BaseTable/baseTablecheck"
export default {
  components: { baseTablecheck },
  data () {
    return {
      searchForm: {
        shipvoyageId: '',
      },
      selectCheck: [],
      tableData: [],
      columns: [
        {
          prop: 'workDate',
          label: '日期',
        },
        {
          prop: 'workTime',
          label: '班次',
          render: (h,params) => {
            if (params.row.workTime == '01') {
              return h('span', {
                domProps: {
                  innerHTML: '白班'
                }
              })
            } else if (params.row.workTime == '02') {
              return h('span', {
                domProps: {
                  innerHTML: '晚班'
                }
              })
            }
          }
        },
        {
          prop: 'processName',
          label: '作业过程',
        },
        {
          prop: 'shipvoyageName',
          label: '船名航次',
          width: 120
        },
        {
          prop: 'cargoName',
          label: '货名',
        },
        {
          prop: 'quantity',
          label: '件数',
        },
        {
          prop: 'ton',
          label: '重量',
        },
        {
          prop: 'cargoOwnerName',
          label: '货主',
        },
        {
          prop: 'cargoAgentName',
          label: '货代',
        },
      ],
      tableHeight: window.innerHeight - 210,
      beforeAdject: '',
      afterAdject: '',
      shipList: [],
      ischecked: false,
      loading: false,
    }
  },
  activated () {
    this.getShipList()
  },
  methods: {
    // 查询船名航次
    getShipList () {
      publicapi.getListShipvoyage().then(res => {
        this.shipList = res.data.data
      })
    },
    // 查询主列表
    searchData () {
      if (!this.searchForm.shipvoyageId) {
        return this.$message.warning('选择船舶后再进行查询')
      }
      this.loading = true
      api.queryByShipvoyageId(this.searchForm.shipvoyageId).then(res => {
        if (res.data.code != '0000') {
          return this.$message.error(res.data.msg)
        }
        this.loading = false
        this.tableData = res.data.data
        if (this.$refs.tableData)
          this.$refs.tableData.clearSelection()
      })
    },
    // 自动调整
    autoAdject () {
      let params = {
        adjustBefore: this.beforeAdject,
        adjustAfter: this.afterAdject,
        isTrue: this.ischecked ? 'Y' : 'N',
        list: this.selectCheck
      }
      api.updatebyid(params).then(res => {
        if (res.data.code != '0000') {
          return this.$message.error(res.data.code)
        }
        this.$message.success('调整成功')
        this.searchData()
        this.beforeAdject = ''
        this.afterAdject = ''
        this.ischecked = false
      })
    },
    // 选择复选框
    selectChange (val) {
      this.selectCheck = val
      var num = 0
      val.map(v => {
        num = num + parseFloat(v.ton ? v.ton : 0)
      })
      this.beforeAdject = num.toFixed(3)
    }
  }
}
</script>

<style lang="less" scoped>
@import "../../../assets/searchfrom.less";
</style>